﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bigger.Study.ConsoleApp.Autofac.Property_Injection
{
    public class PropertyInjectionExample : IExample
    {
        /*
        Property Injection
        If the component is a lambda expression component, use an object initializer:

        builder.Register(c => new A { B = c.Resolve<B>() });
        To support circular dependencies, use an activated event handler:

        builder.Register(c => new A()).OnActivated(e => e.Instance.B = e.Context.Resolve<B>());
        If the component is a reflection component, use the PropertiesAutowired() modifier to inject properties:

        builder.RegisterType<A>().PropertiesAutowired();
        If you have one specific property and value to wire up, you can use the WithProperty() modifier:

        builder.RegisterType<A>().WithProperty("PropertyName", propertyValue); 
        */
        private IContainer Container { get; set; }
        public void Execute()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<ConsoleOutput>().As<IOutput>();

            //自动注入属性
            builder.RegisterType<TodayWriter>().As<IDateWriter>().PropertiesAutowired();
            //builder.RegisterType<TodayWriter>().As<IDateWriter>().WithProperty("Output", new ConsoleOutput());

            Container = builder.Build();


            var writer = Container.Resolve<IDateWriter>();
        }
    }
}
